<template>
  <div class="app-container">
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="120px"
      :inline="false"
      size="small"
    >
      <el-form-item label="借款额度" prop="borrowAmount">
        <el-input-number v-model="form.borrowAmount"></el-input-number>
      </el-form-item>
      <el-form-item label="积分区间开始" prop="integralStart">
        <el-input-number v-model="form.integralStart"></el-input-number>
      </el-form-item>
      <el-form-item label="积分区间结束" prop="integralEnd">
        <el-input-number v-model="form.integralEnd"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="goList">返回列表</el-button>
        <el-button size="small" type="primary" @click="handleSave"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import commonApi from '@/api/common'
import getDetailMix from '@/mixins/getDetailMix'
export default {
  name: 'integralGradeSave',
  data() {
    return {
      apiName: '/admin/core/integralGrade',
      infoApi: commonApi.info,
      form: {
        id: '',
        borrowAmount: 0,
        integralStart: 0,
        integralEnd: 0,
      },
      rules: {
        borrowAmount: [{ required: true, message: '不能为空', trigger: blur }],
        integralStart: [{ required: true, message: '不能为空', trigger: blur }],
        integralEnd: [{ required: true, message: '不能为空', trigger: blur }],
      },
    }
  },
  mixins:[ getDetailMix ],
  watch: {
    $route(newValue, oldValue) {
      this.form = {
        id: '',
        borrowAmount: 0,
        integralStart: 0,
        integralEnd: 0,
      }
    }
  },
  created () {
    if(this.$route.query.id){
      this.form.id = this.$route.query.id
      this.getDetails(this.apiName,this.form.id)
    }
  },
  
  methods: {
    goList() {
      this.$router.push('/core/integral-grade/list')
    },
    // 处理新增或修改
    handleSave() {
      this.$refs.form.validate((v) => {
        if(v){
          if(this.form.id){
            this.handleEdit()
          }else{
            this.handleAdd()
          }
        }
      })
    },
    async handleAdd() {
      let res = await commonApi.save(this.apiName, this.form)
      if (res.code === 0) {
        this.$message.success('新增数据成功')
        this.goList()
      }
    },
    async handleEdit() {
      let res = await commonApi.update(this.apiName, this.form)
      if (res.code === 0) {
        this.$message.success('修改数据成功')
        this.goList()
      }
    },
  },
}
</script>

<style lang="scss" scoped>
</style>